package com.ef.web.action;

import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.ef.constant.ConfirmMessage;
import com.ef.domain.PTE;
import com.ef.domain.Team;
import com.ef.domain.User;
import com.ef.service.PTEService;
import com.ef.util.CustomExample;

public class PTEAction extends BaseAction<PTE> {

	private static final long serialVersionUID = 1L;
	private PTEService pTEService;

	protected final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
			.getLog(PTEAction.class);

	@SuppressWarnings("serial")
	public String list() {

		this.listResult = pTEService.findByExample(new CustomExample<PTE>(this
				.getModel()) {
			public void appendCondition(Criteria criteria) {
				User user = getSessionUser();
				Team team = null;
				if (user.getRole() != null
						&& user.getRole().getName().trim().equalsIgnoreCase(
								"MK")) {
					team = user.getTeam();
				}
				if (team != null) {
					criteria.add(Restrictions.eq("team", team));
				}
				criteria.addOrder(Order.asc("worknumber"));
				this.enableLike(MatchMode.ANYWHERE);
			}
		}, this.getStartIndex(), this.getPageSize());
		return LIST;
	}

	public String edit() {
		return EDIT;
	}

	public String save() {
		PTE pte = this.getModel();
		PTE lll = pTEService.findByDuplicatePte(pte);
		if (lll != null) {
			StringBuffer info = new StringBuffer();
			info.append(lll.getName()).append(" has the same NO!");
			resultError(info.toString(), "list");
			return EDIT;
		}
		String name = pte.getName();
		User user = this.getSessionUser();
		if (user.getRole() != null && user.getRole().isMK()) {
			if (user.getTeam() != null) {
				String ac = user.getTeam().getAdultOrChild();
				if (ac.length() > 0 && name.trim().indexOf(ac) < 0) {
					pte.setName(name + ac);
					pte.setTeam(user.getTeam());
				}
			}
		}
		pte.setWorknumber(pte.getWorknumber().trim());
		pte.setUpdateTime(new Date());
		pte.setUpdateUser(getSessionUser());
		pTEService.saveOrUpdate(pte);
		this.setResult(ConfirmMessage.PTE);
		return EDIT;
	}

	public String delete() {
		String ids = getRequest().getParameter("ids");
		String[] id = ids.split(",");
		for (String i : id) {
			pTEService.delete(pTEService.findById(Long.valueOf(i)));
		}

		return DELETE;
	}

	public PTEService getpTEService() {
		return pTEService;
	}

	public void setpTEService(PTEService pTEService) {
		this.pTEService = pTEService;
	}

	// TODO 中文过滤
	public String autoCompleteAjax() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		response.setHeader("Cache-Control", "no-cache");
		response.setContentType("text/plain;charset=GBK ");
		PrintWriter out = null;
		try {
			String q = request.getParameter("q");
			String limit = request.getParameter("limit");
			List<PTE> list=null;
			User user = this.getSessionUser();
			if (user.getRole() != null && user.getRole().isMK()) {
				if (user.getTeam() != null) {
					list = pTEService.findByAnythingWithTeam(q, Integer
							.valueOf(limit),user.getTeam());
				}else{
					list = pTEService.findByAnything(q, Integer
							.valueOf(limit));
				}
			}else{
				list = pTEService.findByAnything(q, Integer
						.valueOf(limit));
			}
			 
			// {id:id,name:name}
			String responseStr = "";
			// System.out.println(q);
			out = response.getWriter();
			for (PTE key : list) {
				responseStr = ("{id:") + (key.getId()) + (",name:\"")
						+ (key.getName()) + ("\",wno:\"")+key.getWorknumber()+("\"}");
				// System.out.println(responseStr);
				out.println(responseStr);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (Exception e) {

				}
			}
		}
		return null;
	}
}
